/**
 * 
 */
package com.wsj.util.tool;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author fengbin
 *  
 *  静态页面生成对象
 */
public class HtmlCreateObj 
{
//	默认的文件路径
	private  String filePath="html"+File.separator+"products"+File.separator; 
	
	//默认的文件名称
	private String fileName="default.html";
	
	//需要生成的文件链接
	private String  url="http://sale.qqddc.com/buyCarF.do?action=tobuyproducts&productId=168";
	
	 private static Log logger = LogFactory.getLog(HtmlCreateObj.class);
	
	/***
	 * 设置文件路径,设置文件名称
	 * @param filePath
	 * @param fileName
	 * 
	 * url :是需要静态话的链接.filePath: 是生成静态页的路径,fileName是生成静态页的名字.
	 * 比如:
	 *    url:"http://sale.qqddc.com/buyCarF.do?action=tobuyproducts&productId=168"
	 *    filePath="html/products/";
	 *    fileName="p12.html";
	 *   
	 */
	public void setCreateFileParament(String  url , String filePath,String fileName)
	{
		if(url!=null && !url.equals(""))
		{
			this.url=url;
		}
		
		if(fileName!=null &&  !fileName.equals(""))
		{
			this.fileName=fileName;
		}
		
		if(filePath!=null  && !filePath.equals(""))
		{
			this.filePath=filePath;
		}
		
		
	}
	
	
    /*****
     * 通过链接创建html页面
     *  如果文件存在 则覆盖掉.
     *  
     * @param request
     * @param response
     * @param link
     * @return
     */
	public boolean createHtmlFile(HttpServletRequest request,HttpServletResponse response) 
	{
		
		boolean isok=false;
		
		InputStream  inputStream  = null;
		BufferedOutputStream out = null;
		
		try {
			
			//生成文件的路径
			String newFilePath=this.createNewFilePath(request);
			
			//检查生成的文件路径是否存在,如果不存在.则创建该文件路径.
			  File fileDir=new File(newFilePath);
				
			  //该文件夹不存在,则创建文件夹.
				if(!fileDir.exists())
				{
					if(!fileDir.mkdirs())
					{
						logger.info("\n\n\n文件夹创建失败");
					}
				}
 
			URL conUrl = new URL(url); 
            URLConnection connection = conUrl.openConnection(); 
            connection.connect(); 
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection; 
            int httpResult = httpURLConnection.getResponseCode(); 
            if (httpResult != HttpURLConnection.HTTP_OK)
            { 
            	logger.info("没有连接成功"); 
            }
            else 
            { 
            
            	  inputStream  = new BufferedInputStream(httpURLConnection.getInputStream());
            	
            	  // 写入到数据流
				  out=new BufferedOutputStream(new FileOutputStream(newFilePath+fileName));
				 //设置流数据包的大小
				 byte[] buffer=new byte[64 * 1024];
					int length;
					while((length=inputStream.read(buffer,0,buffer.length))>0)
					{
						out.write(buffer,0,length);
					}
					
					
					logger.error("\n\n 创建文件:"+newFilePath+fileName+"成功!");
            } 

			
			isok=true;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				//清除缓存
				if(out != null){
					out.flush();
					out.close();
				}			
				if(inputStream != null){
					inputStream.close();
				}				
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
				logger.error("\n\n关闭流异常!");
			}
			
		}
        
        
		return isok;
        
       }

	
	/****
	 * 获取新的生成文件的链接
	 * @param request
	 * @return
	 */
	private String createNewFilePath(HttpServletRequest request) 
	{
		 String d=File.separator;
		 String fileRealPath="";
		 String retFilePath="";
		 
		  if(d.equals("\\"))//windows
		  {
			  fileRealPath=request.getSession().getServletContext().getRealPath("\\");;
		  }
		  else
		  {
			  fileRealPath=request.getSession().getServletContext().getRealPath("/");;
		  }
		 
		  retFilePath=fileRealPath+filePath;
			
			return retFilePath; 
	}

	
	
	

	public String getFileName() {
		return fileName;
	}


	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


	public String getFilePath() {
		return filePath;
	}


	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	} 

		

}
